addEvent("onload", init);
addEvent("onresize", checkOrientation);
var channelmonitoractive = false;

var screenmode = 0;
var label_start_monitor_label = "START";
var label_stop_monitor_label = "STOP";
var initial_value = "";



function init(){
    try {
        document.getElementById("MonitorToggle").innerHTML = label_start_monitor_label;
        var myStyleTweaks = new StyleTweaker();
        myStyleTweaks.add("Series60/5.0", "styles/tweaks/S605th.css");
        myStyleTweaks.add("Series60/3.2", "styles/tweaks/S603rdFP2.css");
        myStyleTweaks.tweak();
        
        initSystemServices();
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}


function checkOrientation(){
    try {
        var width = parseInt(screen.width);
        var height = parseInt(screen.height);
        
        //var landscapeTweaker = new StyleTweaker();
        // Landscape tweaks for template styles not available in templates?
        //landscapeTweaker.add("Series60/5.0", "../resources/styles/tweaks/S605thLandscape.css");
        
        if (width > height) {
        
            // if already in landscape mode, return
            if (screenmode == 1) 
                return;
            screenmode = 1;
            // landscapeTweaker.tweak();
        }
        else {
            // if already in portrait mode, return
            if (screenmode == 0) 
                return;
            screenmode = 0;
            //        landscapeTweaker.untweak();
        }
    } 
    
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Onclick handler for start/stop monitoring button
 Starts and stops position monitoring
 */
function MonitorToggle_onclick(actionobj){
    try {
        if (channelmonitoractive) {
            actionobj.innerHTML = label_start_monitor_label;
            stopMonitoring();
        }
        else {
            actionobj.innerHTML = label_stop_monitor_label;
            startMonitoring();
        }
        channelmonitoractive = !channelmonitoractive;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 Displays position data in widget ui.
 result object is passed to display function in
 service platform asyncronous callback handlers. Schema:
 Position
 {
 "coords": Coordinates ,
 "timestamp": Date
 }
 Coordinates
 {
 "latitude": number,
 "longitude": number,
 "altitude": number,
 "accuracy": number,          //currently Not Supported
 "altitudeAccuracy": number,  //currently Not Supported
 "heading": number,
 "speed": number,
 }
 */
function displayPositionData(result){
    try {
        document.getElementById("lat").innerHTML = round(result.coords.latitude) + " \u00B0";
        document.getElementById("lon").innerHTML = round(result.coords.longitude) + " \u00B0";
        if (result.coords.altitude != null && !isNaN(result.coords.altitude)) 
            document.getElementById("alt").innerHTML = Math.round(result.coords.altitude) + " m";
        if (result.coords.heading != null) 
            document.getElementById("heading").innerHTML = result.coords.heading + " \u00B0";
        if (result.coords.speed != null) 
            document.getElementById("speed").innerHTML = result.coords.speed + " m/s";
		if (result.timestamp != null)
			document.getElementById("time").innerHTML = formatDateTime(result.timestamp);
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 This function puts initial label for all elements in UI
 */
function UIReset(){
    try {
        document.getElementById("lat").innerHTML = initial_value;
        document.getElementById("lon").innerHTML = initial_value;
        document.getElementById("alt").innerHTML = initial_value;
        document.getElementById("heading").innerHTML = initial_value;
        document.getElementById("speed").innerHTML = initial_value;
        document.getElementById("time").innerHTML = initial_value;
    } 
    catch (e) {
        var error = e.toString();
        alert(error);
    }
}

/*
 * Rounds number
 */
function round(v){
    var a = 100000;
    return Math.round(v * a) / a;
}


/*
 * Formats JS Date object in form: 'dd/MM/yyyy hh:mm'
 * date - date object to be formated
 */
function formatDateTime(date){
	
	if (typeof date == "string") {
		return date;
	}
	else {
		var separator = "/";
		return preformatNumber(date.getDate()) + separator + preformatNumber((date.getMonth() + 1)) + separator + date.getFullYear() + " " + preformatNumber(date.getHours()) + ":" + preformatNumber(date.getMinutes()) + ":" + preformatNumber(date.getSeconds());
	}
}

/*
 * Adds extra '0' in front of number if it is < 10
 */
function preformatNumber(n){
    return n < 10 ? "0" + n : n;
}
